﻿// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.ApplicationModel.Resources;
using WeAppsCenter.Core;
using Windows.Storage;
using Windows.Storage.Pickers;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace WeAppsCenter.Pages;
/// <summary>
/// 设置页面
/// </summary>
public sealed partial class Settings : Page
{
    LicensesWindow licensesWindow;
    public Settings()
    {
        this.InitializeComponent();
        Get();
        ResourceLoader resourceLoader = new ResourceLoader();
        About.Header = resourceLoader.GetString("AppName/Text");
        About.Description = $"© {DateTime.Today.Year} ZHONE and We Apps. All rights reserved.";
        About.Content = CheckForSoftwareUpdates.LocalVersion;
        BitmapIcon bitmapIcon = new BitmapIcon() { UriSource = new Uri($"ms-appx:///Assets/weico.png"), ShowAsMonochrome = false };
        About.HeaderIcon = bitmapIcon;
    }


    public void Save()
    {
        Core.Settings.DefaultDirectory = DefaultDirectory.Text;
        if (AutoUpdate.IsOn)
        {
            Core.Settings.AutoUpdate = true;
        }
        else
        {
            Core.Settings.AutoUpdate = false;
        }


    }
    public void Get()
    {
        DefaultDirectory.Text = Core.Settings.DefaultDirectory;
        AutoUpdate.IsOn = Core.Settings.AutoUpdate;
    }

    private void DefaultDirectory_TextChanged(object sender, TextChangedEventArgs e)
    {
        Save();
    }

    private void AutoUpdate_Toggled(object sender, RoutedEventArgs e)
    {
        Save();
    }

    private async void Browse_Click(object sender, RoutedEventArgs e)
    {

        FolderPicker folderPicker = new FolderPicker();
        // 通过传入Window对象获取当前窗口的HWND
        var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.m_window);
        // 将HWND与文件选择器相关联
        WinRT.Interop.InitializeWithWindow.Initialize(folderPicker, hwnd);
        folderPicker.FileTypeFilter.Add("*");
        StorageFolder storageFolder = await folderPicker.PickSingleFolderAsync();
        if (storageFolder != null)
        {
            DefaultDirectory.Text = storageFolder.Path;
        }
    }

    private void OpenLicenses_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            licensesWindow.Activate();
        }
        catch
        {
            licensesWindow = new LicensesWindow();
            OpenLicenses_Click(null, null);
        }
    }

    private void About_Expanded(object sender, EventArgs e)
    {
        ResourceLoader resourceLoader = new ResourceLoader();

        if (!CheckForSoftwareUpdates.GetIfNewerVersion())
        {
            CheckForUpdate.Header = resourceLoader.GetString("AppUpToDate");
            CheckForUpdatesButton.IsEnabled = false;
        }
        else
        {
            CheckForUpdate.Header = string.Format(resourceLoader.GetString("NewVersionDetected"), CheckForSoftwareUpdates.NewVersion);
            CheckForUpdatesButton.IsEnabled = true;
        }
    }

    private void CheckForUpdatesButton_Click(object sender, RoutedEventArgs e)
    {
        Process.Start("WeAppsCenterOnlineSetup.exe");
    }
}
